#include <bits/stdc++.h>
// 2024/12/21
// tag: Mooyo Mooyo
using namespace std;

int n, k, cnt;
char mooyo[101][101], color;
bool vis[101][101];

void dfs(int x, int y){
    if (x < 1 || x > n || y < 1 || y > 10 || mooyo[x][y] != color || vis[x][y])
        return;
    vis[x][y] = true, cnt++;
    dfs(x - 1, y);
    dfs(x + 1, y);
    dfs(x, y - 1);
    dfs(x, y + 1);
}

void dfs2(int x, int y){
    if (x < 1 || x > n || y < 1 || y > 10 || mooyo[x][y] != color)
        return;
    mooyo[x][y] = '0', cnt--;
    dfs2(x - 1, y);
    dfs2(x + 1, y);
    dfs2(x, y - 1);
    dfs2(x, y + 1);
}

signed main()
{
    cin >> n >> k;
    for (int i = 1; i <= n; i++)
        cin >> mooyo[i] + 1;
    while (true){
        bool flag = false;
        memset(vis, 0, sizeof(vis));
        for (int i = 1; i <= n; i++){
            for (int j = 1; j <= 10; j++){
                if (vis[i][j] || mooyo[i][j] == '0')
                    continue;
                color = mooyo[i][j];
                cnt = 0;
                dfs(i, j);
                if (cnt >= k)
                    flag = true, dfs2(i, j);
            }
        }
        if (!flag)
            break;
        for (int i = 1; i <= 10; i++){
            int top = n;
            for (int j = n; j >= 1; j--){
                if (mooyo[j][i] == '0')
                    continue;
                mooyo[top--][i] = mooyo[j][i];
            }
            while (top >= 1)
                mooyo[top--][i] = '0';
        }
    }
    for (int i = 1; i <= n; i++)
        cout << mooyo[i] + 1 << endl;
    return 0;
}
